package controllers

import (
	"encoding/json"
	"shudao-chat-go/models"
	"github.com/beego/beego/v2/server/web"
)

type ReModifyQuestionRequest struct {
	AIConversationID uint64 `json:"ai_conversation_id"`
	Content string `json:"content"`
}

type ExamController struct {
	web.Controller
}

//重新修改考试题目
func (c *ExamController) ReModifyQuestion() {
	//post请求获取ai_conversation_id
	var AIConversationID ReModifyQuestionRequest
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &AIConversationID); err != nil {
		c.Data["json"] = map[string]interface{}{
			"statusCode": 400,
			"msg":        "JSON解析失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	ai_conversation_id := AIConversationID.AIConversationID
	content := AIConversationID.Content
	//修改ai_message表中的ai_conversation_id中的"ai"回答的content
	tx := models.DB.Begin()
	if err := tx.Model(&models.AIMessage{}).Where("ai_conversation_id = ? AND type = 'ai'", ai_conversation_id).Update("content", content).Error; err != nil {
		tx.Rollback()
		c.Data["json"] = map[string]interface{}{
			"statusCode": 500,
			"msg":        "修改失败: " + err.Error(),
		}
		c.ServeJSON()
		return
	}
	tx.Commit()
	c.Data["json"] = map[string]interface{}{
		"statusCode": 200,
		"msg":        "success",
	}
	c.ServeJSON()
}